package com.zxc.controller.diseases;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxc.dao.DiseasesDao;
import com.zxc.model.*;
import com.zxc.model.Diseases.DiseasesHistory;
import com.zxc.model.Diseases.DiseasesVO;
import com.zxc.service.DiseasesService;
import com.zxc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin
@RestController
@RequestMapping("/diseases")
public class DiseasesController {
    @Autowired
    private DiseasesService diseasesService;

    @Autowired
    private UserService userService;




    @RequestMapping("/findTreatmentByDiseasesNo")
    public ResponseEntity<?> gettreatmentbydiseases(@RequestBody  DiseasesVO vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =vo.getPageList().get(0);
        PageHelper.startPage(page.getOffset(), page.getLimit());
        response.put("DiseasesList",new PageInfo<DiseasesVO>(diseasesService.findTreatmentByDiseasesNo(vo)));
        return ResponseEntity.ok(response);
    }

    @RequestMapping("/findTreatmentByDiseasesHistory")
    public ResponseEntity<?> gettreatmentbydiseases(@RequestBody DiseasesHistory vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =vo.getPageList().get(0);
        vo.setUserId(userService.getUser(page.getAccount()).getId());
        PageHelper.startPage(page.getOffset(), page.getLimit());
        response.put("DiseasesHistoryList",new PageInfo<DiseasesHistory>(diseasesService.findTreatmentByDiseasesHistory(vo)));
        return ResponseEntity.ok(response);
    }

    @RequestMapping("/findTreatmentByDiseasesById")
    public ResponseEntity<?> findTreatmentByDiseasesById(@RequestBody  DiseasesVO vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("DiseasesList",diseasesService.findTreatmentByDiseasesById(vo));
        return ResponseEntity.ok(response);
    }

    //传入一个对象，有多个属性的值
    @RequestMapping("/selectTreatmentByDiseases")
    public ResponseEntity<?> selecttreatmentbydiseases(@RequestBody DiseasesVO diseasesVO) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page = diseasesVO.getPageList().get(0);
        PageHelper.startPage(page.getOffset(), page.getLimit());
//        添加数据
        response.put("CropsList",new PageInfo<DiseasesVO>(diseasesService.selecttreatmentbydiseases(diseasesVO)));
        return ResponseEntity.ok(response);
    }

    //插入病症历史表
    @RequestMapping("/insertDiseasesHistory")
    public ResponseEntity<?> insertDiseasesHistory(@RequestBody DiseasesHistory dh) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =dh.getPageList().get(0);
        String id =userService.getUser(page.getAccount()).getId();
        dh.setUserId(id);
        userService.setDiseasesHistory(dh);
        return ResponseEntity.ok(response);
    }

    //测试：只是插入主键
    @RequestMapping("/test")
    public ResponseEntity<?> test(@RequestBody DiseasesVO diseasesVO) {
        Map<String, Object> response = new HashMap<String, Object>();
        diseasesService.insertTestId(diseasesVO);
        return ResponseEntity.ok(response);
    }

}
